import re

class Sectioner:    
    sectionDict = {'ch':"<Title>CLINICAL HISTORY</Title>",
                   'mac':"<Title>MACROSCOPIC</Title>",
                   'mic':"<Title>MICROSCOPIC</Title>",
                   'con':"<Title>CONCLUSION</Title>",
                   'none':None}
    titleTagDict = {"CLINICAL HISTORY":"1",
                   "MACROSCOPIC":"2",
                   "MICROSCOPIC":"3",
                   "CONCLUSION":"4"}

    def __init__(self, secKey):
        self.secTitle = self.getSectionHead(secKey)
        if self.secTitle != None:
            pattern = r"(%s.*?)(?:<Title>|$)"%self.secTitle
            self.secMatcher = re.compile(pattern,re.DOTALL)
        self.sntPatt = re.compile(r"(.+?)(?:[\.!?\n]|$)")
        self.titlePatt = re.compile(r"<Title>(.+?)</Title>", re.IGNORECASE| re.DOTALL)
        self.setPattDict(self.titleTagDict)
        return
    
    def getSectionHead(self, sectionKey):
        return self.sectionDict[sectionKey.lower()]
        
    def getSectionList(self, text):
        if self.secTitle!= None:
            matchdStrList = self.secMatcher.findall(text)
            return matchdStrList
        else:
            return None
        
    def getTitleNames(self, text):
        titleList = self.titlePatt.findall(text)
        return titleList
        
    def getTitleTag(self, title):
        if title in self.titleTagDict:
            return self.titleTagDict[title]
        else:
            return '0'
        
    def getSectionsByTitle(self, text, title):
        if title not in self.pattDict:
            try:
                pattern = r"(?:<Title>%s</Title>)(.*?)(?:<Title>|$)"%title            
                #pattern = re.escape(pattern)
                self.pattDict[title] = re.compile(pattern,re.DOTALL)
            except Exception:
                pattern = "(?:<Title>%s</Title>)(.*?)(?:<Title>|$)"%title            
                pattern = re.escape(pattern)
                self.pattDict[title] = re.compile(pattern,re.DOTALL)                
        patt = self.pattDict[title]
        return patt.findall(text)    
    
    def setPattDict(self, titleTagDict):
        self.pattDict = {}
        for title in titleTagDict:
            pattern = r"(?:<Title>%s</Title>)(.*?)(?:<Title>|$)"%title
            print pattern
            #pattern = re.escape(pattern)
            self.pattDict[title] = re.compile(pattern,re.DOTALL)
            

    def blockSection(self, text):
        strList = self.getSectionList(text)
        if strList:
            for st in strList:
                text = text.replace(st, ' ')
        return text

    def getParagraphs(self, textStr):
        paraList = textStr.split('\n')
        while paraList.count('') > 0:
            paraList.remove('')
        return paraList

    def getSentences(self, textStr):
        return self.sntPatt.findall(textStr)
    
    def getFullText(self, textStr):
        return [textStr]        
    
if __name__ == "__main__":
    noneSectioner = Sectioner('None')
    text = open('../pathotext.txt','rU').read()
    titles = noneSectioner.getTitleNames(text)
    for title in titles:
        print title
        print noneSectioner.getSectionsByTitle(text, title)
